<template>
  <!-- 添加或修改盘点计划对话框 -->
  <el-dialog :title="title" v-if="open" v-model="open" width="600px" append-to-body>
    <el-row :gutter="20">
      <el-col :span="24" :offset="0">
        <el-form ref="form" :model="form" :rules="rules" label-width="130px">
          <el-form-item label="模板名称:" prop="pptName">
            <el-input v-model="form.pptName"></el-input>
          </el-form-item>
          <el-form-item label="选择主题:" prop="groupId">
            <el-select v-model="form.groupId" value-key="" placeholder="" clearable filterable>
              <el-option v-for="item in fodders" :key="item.id" :label="item.groupName" :value="item.id"> </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="缩略图:" prop="pptTopImg" class="goods_imgs">
            <el-upload
              class="avatar-uploader el-upload"
              :headers="headers"
              accept="image/*"
              :on-success="(res, file) => handleUploadSuccess(res, file, 'pptTopImg')"
              :show-file-list="false"
              :action="uploadUrl"
            >
              <el-image v-if="form.pptTopImg" class="avatar" :src="form.pptTopImg" lazy>
                <template #error>
                  <div class="image-slot">
                    <img class="avatar" src="@/assets/goods/order/img_failure.svg" alt="" />
                  </div>
                </template>
              </el-image>
              <div v-else class="upload-text">点击上传主图</div>
              <div class="btn" v-if="form.pptTopImg">
                <el-button type="primary" @click.stop="deleteImg('pptTopImg')">
                  <el-icon>
                    <Delete />
                  </el-icon>
                </el-button>
              </div>
            </el-upload>
          </el-form-item>

          <el-form-item label="模板数据:" prop="remark">
            <el-input type="textarea" :rows="6" v-model="form.remark"></el-input>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm" :loading="loading">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import modelMixin from '@/mixins/modelMixin';
import materialPpt from '@/api/goods/materialPpt';
import { listMaterialPptGroup } from '@/api/goods/materialPptGroup';
import { getToken } from '@/utils/auth';
import { globalHeaders } from '@/utils/request';
export default {
  name: 'uploadForm',
  mixins: [modelMixin],
  props: {
    classifyList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      uploadUrl: import.meta.env.VITE_APP_BASE_API + '/resource/oss/upload', // 上传的图片服务器地址
      headers: globalHeaders(),
      funcObj: materialPpt, //方法对象
      open: false,
      currentChildIndex: 0,
      title: '上传图片',
      childTabs: [
        { label: '左对齐', left: '10%' },
        { label: '居中', left: 'calc(50% - 50px)' },
        { label: '右对齐', left: 'calc(90% - 100px)' }
      ],
      form: {},
      fodders: [],
      rules: {
        groupId: [{ required: true, message: '请选择分组', trigger: 'change' }],
        remark: [{ required: true, message: '模板内容不能为空', trigger: 'blur' }],
        pptName: [{ required: true, message: '模板名称不能为空', trigger: 'blur' }],
        remark: [{ validator: this.validateContent, trigger: 'change', reuqired: true }]
      }
      // classifyList: [],
    };
  },
  created() {
    // this.getClassifyAllList()
  },
  methods: {
    add(row = {}) {
      console.log('row :>> ', row);
      this.open = true;
      this.getGroupList();
      this.form = row;
      this.title = '新增PPT模板';
    },
    edit(row) {
      this.open = true;
      this.getGroupList();
      this.form = JSON.parse(JSON.stringify(row));
      this.title = '编辑PPT模板';
      console.log('row :>> ', row);
    },
    classifyForm() {
      this.form.materialType = 0;
    },

    getGroupList() {
      listMaterialPptGroup()
        .then((res) => {
          if (res.code == 200) {
            this.fodders = res.rows;
          } else {
            this.$modal.msgError(res.msg);
          }
        })
        .catch((err) => {
          console.log('err :>> ', err);
        });
    },

    childClick(item, index) {
      this.currentChildIndex = index;
    },
    // 上传成功
    handleUploadSuccess(res, file, key) {
      // 如果上传成功
      if (res.code == 200) {
        this.form[key] = res.data.url;
      } else {
        console.log(res);
      }
    },

    // 删除图片
    deleteImg(key) {
      this.form[key] = '';
    },

    validateContent(rule, value, callback) {
      try {
        let content = JSON.parse(value);
        callback();
      } catch (e) {
        callback(new Error('请输入正确的JSON模板数据'));
      }
    }
  }
};
</script>
<style type="text/css" lang="scss" scoped>
.avatar-uploader,
.el-upload {
  border: 1px solid #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 178px !important;
  height: 178px;
  display: flex;
  margin-right: 10px;
}

.avatar-uploader:hover,
.el-upload:hover {
  border-color: #409eff;
}

.imgs {
  display: inline-block;
}

.avatar-uploader__img {
  border: 1px solid #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 178px !important;
  height: 178px;
  display: inline-block;
  margin-right: 10px;

  img {
    width: 178px !important;
    height: 178px;
    object-fit: fill;
  }
}

.btn {
  display: none;
  position: absolute;
  top: 0;
  right: 0;
  background-color: rgba(0, 0, 0, 0.5);
  color: #fff;
  width: 178px !important;
  height: 178px;
  border-radius: 0 6px 0 6px;
  text-align: center;
  line-height: 178px;
}

.avatar-uploader:hover {
  // 鼠标悬浮显示按钮
  .btn {
    display: block;
  }
}

.upload-text {
  font-size: 14px;
  color: #8c939d;
  line-height: 178px;
  text-align: center;
  margin: 0 auto;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.child_tabs {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  margin-top: 10px;
  height: 34px;

  .tab {
    height: 34px;
    padding: 0px 10px;
    font-size: 12px;
    text-align: center;
    width: 72.33px;
    cursor: pointer;
    line-height: 34px;
    text-align: center;

    &:hover {
      background-color: #409eff;
      color: #fff;
    }

    border: 1px solid #e8e8e8;
  }

  :not(:last-child) {
    border-right: none;
  }

  :first-child {
    border-top-left-radius: 4px;
    border-bottom-left-radius: 4px;
  }

  :last-child {
    border-top-right-radius: 4px;
    border-bottom-right-radius: 4px;
  }

  .active {
    background-color: #409eff;
    color: #fff;
  }
}

.img-main {
  width: 400px;
}

.img-item {
  width: 400px;
  vertical-align: top;
  object-fit: contain;
}
</style>
<style lang="scss">
.el-form-item__content {
  width: auto !important;
}
</style>